package com.lancoo.klgcourseware.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lancoo.klgcourseware.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by pengmi on 2018/9/17.
 * Description:图文混排中,图片浏览界面
 */

public class KlgImageViewPagerActivity extends AppCompatActivity {

    protected ViewPager viewPager;
    protected TextView tv_index;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.klg_activity_img_view_pager);
        setStatusBarColor(this,0);
        initView();
    }

    private void initView(){
        viewPager = findViewById(R.id.pager);
        tv_index = findViewById(R.id.tv_img_index);
        final ArrayList<String> imgUrls = getIntent().getStringArrayListExtra("imgUrls");
        viewPager.setOffscreenPageLimit(imgUrls.size());
        tv_index.setVisibility(imgUrls.size()>1?View.VISIBLE:View.INVISIBLE);
        Log.i("aaaa","size:"+getIntent().getIntExtra("currentIndex",0));
        viewPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        final RequestOptions requestOptions=new RequestOptions();
//        requestOptions.error(R.mipmap.fsbl_res_unknow);
//        requestOptions.placeholder(R.mipmap.fsbl_res_unknow);
//        requestOptions.fallback(R.mipmap.fsbl_res_unknow);

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgUrls.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view=new PhotoView(KlgImageViewPagerActivity.this);
                view.enable();
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                Log.e("aaaa",position+",instantiateItem"+imgUrls.get(position));

                Glide.with(KlgImageViewPagerActivity.this).load(imgUrls.get(position)).into(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View)object);
            }
        });
        viewPager.setCurrentItem(getIntent().getIntExtra("currentIndex",0));
        setIndexCount(getIntent().getIntExtra("currentIndex",0),imgUrls.size());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setIndexCount(position,imgUrls.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * @param mContext 上下文
     * @param urls 图片url集合
     * @param position  当前选中图片下标
     */
    public static void launch(Context mContext,List<String> urls,int position){
        Intent mIntent=new Intent(mContext,KlgImageViewPagerActivity.class);
        mIntent.putExtra("currentIndex",position);
        mIntent.putStringArrayListExtra("imgUrls",(ArrayList<String>) urls);
        mContext.startActivity(mIntent);
    }

    private void setIndexCount(int currentIndex,int totalIndex){
        String indexString=String.format("%1$d/%2$d",(currentIndex+1),totalIndex);
        SpannableString spannableString=new SpannableString(indexString);
        spannableString.setSpan(new AbsoluteSizeSpan(26,true),0,String.valueOf(currentIndex).length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv_index.setText(spannableString);
    }

    /**
     * 设置标题栏、状态栏
     *
     * @param activity
     * @param color
     */
    protected void setStatusBarColor(Activity activity, int color) {
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ViewGroup decorView = (ViewGroup) window.getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            decorView.setSystemUiVisibility(option);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

}
